# -*- coding:utf8 -*-

import json
from squirrel_core.item import ProcedureItem

__all__ = ["ProcedurePipeline"]


class ProcedurePipeline(object):
    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler)

    def __init__(self, crawler):
        self.upstream_queue = crawler.spider.queues[1]

    def process_item(self, item, spider):
        logger = spider.logger
        if isinstance(item, ProcedureItem):
            _data = item.process_item()
            try:
                self.upstream_queue.put(json.dumps(_data))
                spider.close_spider_when_idle()
            except (BrokenPipeError, ConnectionError) as e:
                logger.info(f"队列写入失败: {e}")
            except Exception as e:
                logger.info(f"处理数据时出错: {e}")
        return item
